1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.testing.WrongType;
24  import com.google.common.collect.testing.features.CollectionFeature;
25  import com.google.common.collect.testing.features.CollectionSize;
26  
27  /**
28   * Common parent class for {@link ListIndexOfTester} and
29   * {@link ListLastIndexOfTester}.
30   *
31   * @author Chris Povirk
32   */
33  @GwtCompatible
34  public abstract class AbstractListIndexOfTester<E>
35      extends AbstractListTester<E> {
36    /** Override to call {@code indexOf()} or {@code lastIndexOf()}. */
37    protected abstract int find(Object o);
38  
39    /**
40     * Override to return "indexOf" or "lastIndexOf()" for use in failure
41     * messages.
42     */
43    protected abstract String getMethodName();
44  
45    @CollectionSize.Require(absent = ZERO)
46    public void testFind_yes() {
47      assertEquals(getMethodName() + "(firstElement) should return 0",
48          0, find(getOrderedElements().get(0)));
49    }
50  
51    public void testFind_no() {
52      assertEquals(getMethodName() + "(notPresent) should return -1",
53          -1, find(samples.e3));
54    }
55  
56    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
57    public void testFind_nullNotContainedButSupported() {
58      assertEquals(getMethodName() + "(nullNotPresent) should return -1",
59          -1, find(null));
60    }
61  
62    @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
63    public void testFind_nullNotContainedAndUnsupported() {
64      try {
65        assertEquals(
66            getMethodName() + "(nullNotPresent) should return -1 or throw",
67            -1, find(null));
68      } catch (NullPointerException tolerated) {
69      }
70    }
71  
72    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
73    @CollectionSize.Require(absent = ZERO)
74    public void testFind_nonNullWhenNullContained() {
75      initCollectionWithNullElement();
76      assertEquals(getMethodName() + "(notPresent) should return -1",
77          -1, find(samples.e3));
78    }
79  
80    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
81    @CollectionSize.Require(absent = ZERO)
82    public void testFind_nullContained() {
83      initCollectionWithNullElement();
84      assertEquals(getMethodName() + "(null) should return " + getNullLocation(),
85          getNullLocation(), find(null));
86    }
87  
88    public void testFind_wrongType() {
89      try {
90        assertEquals(getMethodName() + "(wrongType) should return -1 or throw",
91            -1, find(WrongType.VALUE));
92      } catch (ClassCastException tolerated) {
93      }
94    }
95  }